export interface ImageEntity{

    [src: string]: HTMLImageElement;
}

export class ImageLoader {


    public static loadAll(srcList: string[]): Promise<Map<string, HTMLImageElement>> {

        function load(src: string): Promise<HTMLImageElement> {
            return new Promise(
                (resolve: (image: HTMLImageElement) => void, reject) => {
                    let image = new Image();
                    image.onload = () => {
                        resolve(image);
                    }
                    image.onerror = (event: string | Event) => {
                        reject(event)
                    }
                    image.src = src;
                }
            );
        }


        async function asyncLoad(): Promise<Map<string, HTMLImageElement>>{
            const imageMap: Map<string, HTMLImageElement> = new Map();
            for (const src of srcList) {
                await load(src).then(image => imageMap.set(src,image));
            }
            return imageMap;
        }

       return asyncLoad();
    }

}